<template>
  <div>
    <el-drawer v-model="drawer" title="文件列表" :with-header="true">
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        label-width="120px"
        class="demo-ruleForm"
      >
        <el-form-item label="项目名称：" prop="projectName">
          <el-input v-model="ruleForm.projectName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="项目简称：" prop="projectBrief">
          <el-input v-model="ruleForm.projectBrief" placeholder="请输入项目简称" />
        </el-form-item>
        <el-form-item label="项目类型：" prop="projectType">
          <el-select
            v-model="ruleForm.projectType"
            placeholder="请选择项目类型"
            style="width: 100%"
            filterable
          >
            <el-option
              v-for="ite in projectTypeList"
              :key="ite.value"
              :label="ite.label"
              :value="ite.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="客户名称：" prop="clientId">
          <el-select
            v-model="ruleForm.clientId"
            placeholder="请选择客户名称"
            style="width: 100%"
            filterable
          >
            <el-option
              v-for="ite in clientList"
              :key="ite.id"
              :label="ite.clientName"
              :value="ite.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="实际状态：" prop="actualStatus">
          <el-input v-model="ruleForm.actualStatus" placeholder="请输入实际状态" />
        </el-form-item>
        <el-form-item label="项目金额：" prop="estimatePrice">
          <el-input-number
            v-model="ruleForm.estimatePrice"
            :min="0"
            :max="500000000"
            :precision="2"
            style="width: 240px"
          />
          &nbsp;&nbsp;元
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="drawer = false">取消</el-button>
          <el-button type="primary" @click="handleClose(ruleFormRef)">确定</el-button>
        </span>
      </template>
    </el-drawer>
  </div>
</template>

<script lang="ts" setup>
  import { ref, reactive, nextTick } from 'vue'
  import type { FormInstance } from 'element-plus'
  import { ElMessage } from 'element-plus'
  import { postClientMatch } from '@/api/controller/client-controller/index'
  import { putProject } from '@/api/controller/project-controller/index'
  import { getSysProTypeList } from '@/api/controller/sys-user-controller/index'
  const drawer = ref(false)
  const clientList = ref([])
  const projectTypeList = ref([])
  const show = async (data) => {
    ruleForm.value = { ...data }
    postClientMatch({}).then((res) => {
      clientList.value = res.data
    })
    getSysProTypeList().then((res) => {
      projectTypeList.value = res.data
    })
    drawer.value = true
    nextTick(() => {
      ruleFormRef.value.clearValidate()
    })
  }
  const ruleFormRef = ref<FormInstance>()
  const ruleForm = ref({
    projectName: null,
    projectBrief: null,
    projectType: null,
    projectStatus: null,
    clientName: null,
    clientId: null,
    remark: null,
    actualStatus: null,
    estimatePrice: 0,
  })
  const rules = reactive({
    projectName: [
      { required: true, message: '请输入项目名称', trigger: 'blur' },
      { min: 0, max: 64, message: '长度不超过64个字符', trigger: 'blur' },
    ],
    projectBrief: [
      { required: true, message: '请输入项目简称', trigger: 'blur' },
      { min: 0, max: 64, message: '长度不超过64个字符', trigger: 'blur' },
    ],
    projectType: [
      { required: true, message: '请输入项目类型', trigger: 'blur' },
      { min: 0, max: 64, message: '长度不超过64个字符', trigger: 'blur' },
    ],
    clientId: [{ required: true, message: '请选择客户', trigger: 'blur' }],
    actualStatus: [{ min: 0, max: 64, message: '长度不超过64个字符', trigger: 'blur' }],
  })

  //修改
  const handleClose = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate(async (valid) => {
      if (valid) {
        const result = await putProject(ruleForm.value, { loading: true })
        if (result.code === 200) {
          await emit('getData')
          drawer.value = false
          ElMessage.success('修改成功')
        }
      } else {
        ElMessage.warning('请正确填写表单')
        return false
      }
    })
  }
  const emit = defineEmits(['getData'])
  defineExpose({
    show,
  })
</script>
